package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaControllerApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author 黄子弟
 */
@Slf4j
@RestController
public class MediaController implements MediaControllerApi {


    @Autowired
    private MediaService  mediaService;

    @Override
    @PostMapping("/media")
    public MediaDTO createMedia(@RequestBody MediaVO mediaVO) {
        Long companyId = SecurityUtil.getCompanyId();
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(mediaVO);
        mediaDTO.setCompanyId(companyId);
        MediaDTO dto = mediaService.createMedia(mediaDTO);
        return dto;
    }

    @Override
    @PostMapping("/media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        PageVO<MediaDTO> pageVO = mediaService.queryMediaList(params, model,companyId);
        return pageVO;
    }

    @Override
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        String url = mediaService.previewMedia(mediaId, companyId);
        return url;
    }

    @Override
    @DeleteMapping("/media/{mediaId}")
    public void deleteMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        mediaService.deleteMedia(mediaId,companyId);
    }


}
